<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class AddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // dd(session('id'));
        $id = session('id');
        //实例化操作表
        $ob = DB::table('address')->where('uid',$id)->get();
        // dd($ob);
        // dd($ob);
        // $list = array();
        // $list = $ob ;
         // $list = $ob->paginate(5);
        // dd($ob);
        // foreach ($ob as  $v) {
        //     echo $v;
        // }
        // die();
        return view('home.address',['ob'=>$ob]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // // dd($request);
        $message = [
        // 'telephone.required'=>'电话号码必须填写',
        'telephone.numeric'=>'电话号码必须为数字',
        // 'telephone.max'=>'电话号码不能超过11位',
        ];

        $this->validate($request,[
            'telephone'=>'numeric',
            ],$message);

        // dd($request);
        $data = $request->except('_token');
        //根据三级联动传过来的id数组查询出相应的地址名称
        $address = $request->input('address');
        $a = array();
        $b = array();
        foreach ($request->input('address') as $k => $v) {
            $area = DB::table('district')->where('id',$address[$k])->first();
            $a[] = $area->name;    
            $b[] = $area->id;        
        }
        $data['address'] = implode('', $a);
        $data['aid'] = implode(',', $b);
        //将session中的id 作为uid存入
        $data['uid'] = session('id');
        // dd($data);
        $row = DB::table('address')->insertGetId($data);
        if($row>0){
            return redirect('home/address')->with('msg','添加成功');
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = DB::table('address')->where('id',$id)->first();
        // dd($data->aid); 
        $a = explode(',', $data->aid);
        // dd($a);
        // return view('admin.notice.edit',['ob'=>$data]);
        return view('home.editAddress',['ob'=>$data,'a'=>$a]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($request->except('_token'));
        $data = $request->except('_token','_method');
        // dd($request->input('address'));
        if(count($request->input('address'))>1){
            //如果修改了地址的方法
            //根据传过来的地址数组值
            $address = $request->input('address');
            // dd($address);
            $a = array();
            $b = array();
            foreach ($request->input('address') as $k => $v) {
                $area = DB::table('district')->where('id',$address[$k])->first();
                $a[] = $area->name;    
                $b[] = $area->id;  
                $data['address'] = implode('', $a);
                $data['aid'] = implode(',', $b);     

            }
            // dd($data); 
            $row = DB::table('address')->where('id',$id)->update($data);
            if($row>0){
                return redirect('home/address')->with('msg','修改成功');
            }else{
                return redirect('home/address')->with('error','修改失败');
            }
        }else{
            //没有修改地址的方法
            $data = $request->only('consignee','telephone','detail');
            $row = DB::table('address')->where('id',$id)->update($data);
            // dd($data);
            if($row>0){
                return redirect('home/address')->with('msg','修改成功');
                // return back();
            }else{
                return redirect('home/address')->with('error','修改失败');
            }
        }
       
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $row = DB::table('address')->where('id', $id)->delete();
        if($row > 0){
            // return redirect('home/address')->with('msg','删除成功');
            return back();
        }else{
            return redirect('home/address')->with('error','删除失败');
        }
    }

    public function doget(Request $request)
    {
         // dd(11111);
        $list = DB::table('district')->where('upid',$request->input('upid'))->get();
        // dd('$list');
        echo json_encode($list);
    }

    public function dopost(Request $request)
    {
        $list = DB::table('district')->where('upid',$request->input('upid'))->get();
        // dd('$list');        
        echo json_encode($list);
    }
}
